local Fame = class("Fame")

local string_ulen = string.ulen
local string_usub = string.usub
local SHOW_NAME_MAX_LEN = 6

function Fame:ctor()
	self.itemBagType = Enums.ItemBagType.fame

	self.uid = 0
	self.level = 1
	self.exp = 0

	self.preLev = 1
	self.preExp = 0
end

function Fame:isValid()
	return self.conf ~= nil
end

function Fame:read(value)
	self.preLev = self.level or 1
	self.preExp = self.exp or 0

	self.uid = value.uid
	self.level = value.level
	self.exp = value.exp
	-- print("Fame:read ------ self.uid = ",self.uid)
	-- print("Fame:read ------ self.level = ",self.level)
	-- print("Fame:read ------ self.exp = ",self.exp)

	self.conf = GD:queryFameById(self.uid)
	assert(self.conf ~= nil, "Fame.conf can't be nil " .. self.uid)
	self.quality = tonumber(self.conf.quality or 5)

	self._lvConf = GD:queryFameLevelUpById(self.level)
end

function Fame:setAmount(amount)
	self.amount = amount
end

function Fame:getAmount()
	return self.amount
end

function Fame:getFameAdd()
	local curLev = self.preLev
	local curExp = self.exp
	while curLev < self.level do
		local conf = GD:queryFameLevelUpById(curLev)
		curExp = curExp + tonumber(conf.exp)
		curLev = curLev + 1
	end
	return curExp - self.preExp
end

function Fame:isMaxLv()
	return tonumber(self._lvConf.exp) == 0
end

function Fame:getLvName()
	return L(self._lvConf.name)
end

function Fame:getLvExp()
	return self.exp, tonumber(self._lvConf.exp)
end

function Fame:isOpen()
	return self.level > 1 or self.exp > 0
end

-- isFull true/false 是否取全名
-- maxLen number 自定义截取最大字数
-- 名字默认按6个utf8字符计算，多于6个的截取5个加...
function Fame:getName(isFull, maxLen)
	isFull = isFull or false
	if isFull then
		return L(self.conf.name) .. L("lua_code_text_358")
	else
		maxLen = maxLen or SHOW_NAME_MAX_LEN
		local name = L(self.conf.name) .. L("lua_code_text_358")
		local len = string_ulen(name)
		if len > maxLen then
			name = string_usub(name, 1, maxLen-1) .. "..."
		end
		return name
	end
end

function Fame:getDesc()
	return L(self.conf.des)
end

-- 类型名字
function Fame:getTypeName()
	local showType = self.conf.showType
	if showType and showType ~= "0" then
		return L(Res.showTypeName .. showType)
	end
	return ""
end

function Fame:getQuality()
	return self.quality
end

function Fame:getIconBg()
	return Res.Qulity_Icon_Bg[self.quality]
end

function Fame:getIcon()
	return Res.fameLevelPath .. self.conf.icon .. ".png"
end

return Fame
